using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.TTC;

namespace OracleInternal.ServiceObjects
{
	internal class OracleBFileImpl
	{
		internal OracleConnectionImpl m_connectionImpl;

		internal byte[] m_lobLocator;

		internal TTCBFile m_ttcBFile;

		internal OracleBFileImpl(OracleConnectionImpl connImpl, byte[] lobLocator)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.ctor);
			}
			try
			{
				m_connectionImpl = connImpl;
				m_lobLocator = lobLocator;
				m_ttcBFile = new TTCBFile(m_connectionImpl.m_marshallingEngine);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.ctor, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.ctor);
				}
			}
		}

		internal long GetLength()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.GetLength);
			}
			try
			{
				try
				{
					m_connectionImpl.m_connectionFreeToUseEvent.WaitOne();
					m_connectionImpl.AddAllPiggyBackRequests();
					return m_ttcBFile.GetLength(m_lobLocator);
				}
				finally
				{
					m_connectionImpl.m_connectionFreeToUseEvent.Set();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.GetLength, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.GetLength);
				}
			}
		}

		internal void SetDirFileName(string directoryName, string fileName)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.SetDirFileName);
			}
			try
			{
				m_lobLocator = m_ttcBFile.SetDirFileName(directoryName, fileName);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.SetDirFileName, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.SetDirFileName);
				}
			}
		}

		internal bool OpenFile()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.OpenFile);
			}
			try
			{
				try
				{
					m_connectionImpl.m_connectionFreeToUseEvent.WaitOne();
					m_connectionImpl.AddAllPiggyBackRequests();
					if (m_lobLocator == null)
					{
						m_lobLocator = new byte[20];
					}
					return m_ttcBFile.Open(m_lobLocator, 1);
				}
				finally
				{
					m_connectionImpl.m_connectionFreeToUseEvent.Set();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.OpenFile, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.OpenFile);
				}
			}
		}

		internal bool CloseFile()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.CloseFile);
			}
			bool result = true;
			try
			{
				if (m_lobLocator != null)
				{
					try
					{
						m_connectionImpl.m_connectionFreeToUseEvent.WaitOne();
						if (m_lobLocator != null)
						{
							m_connectionImpl.AddAllPiggyBackRequests();
							result = m_ttcBFile.Close(m_lobLocator);
							m_lobLocator = null;
						}
					}
					finally
					{
						m_connectionImpl.m_connectionFreeToUseEvent.Set();
					}
				}
				return result;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.CloseFile, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.CloseFile);
				}
			}
		}

		internal bool FileExists()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.FileExists);
			}
			try
			{
				try
				{
					m_connectionImpl.m_connectionFreeToUseEvent.WaitOne();
					m_connectionImpl.AddAllPiggyBackRequests();
					return m_ttcBFile.Exists(m_lobLocator);
				}
				finally
				{
					m_connectionImpl.m_connectionFreeToUseEvent.Set();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.FileExists, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.FileExists);
				}
			}
		}

		internal long Read(long locatorOffset, long numBytesToRead, long outBufferOffset, ref byte[] outBuffer)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.Read);
			}
			try
			{
				try
				{
					m_connectionImpl.m_connectionFreeToUseEvent.WaitOne();
					m_connectionImpl.AddAllPiggyBackRequests();
					return m_ttcBFile.Read(m_lobLocator, locatorOffset, numBytesToRead, outBufferOffset, ref outBuffer);
				}
				finally
				{
					m_connectionImpl.m_connectionFreeToUseEvent.Set();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.Read, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.Read);
				}
			}
		}

		internal long CopyTo(byte[] destLobLocator, long srcOffset, long dstOffset, long dataLen)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.CopyTo);
			}
			try
			{
				try
				{
					m_connectionImpl.m_connectionFreeToUseEvent.WaitOne();
					m_connectionImpl.AddAllPiggyBackRequests();
					return m_ttcBFile.Copy(m_lobLocator, destLobLocator, srcOffset, dstOffset, dataLen);
				}
				finally
				{
					m_connectionImpl.m_connectionFreeToUseEvent.Set();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.CopyTo, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleBFileImpl, OracleTraceFuncName.CopyTo);
				}
			}
		}
	}
}
